using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ArgoszLite
{
    public partial class SettingsManager : Form
    {
        private CameraCapture cameraCapture = new CameraCapture();
        Settings setts = Settings.Instance;
   
        public SettingsManager()
        {
            InitializeComponent();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                cameraCapture.Start(0);
                setts.Camera_num++;
                checkBox1.Enabled = true;
                timer1.Enabled = true;

            }
            else
            {
                cameraCapture.Close(0);
                setts.Camera_num--;
                checkBox1.Enabled = false;
                timer1.Enabled = false;
            }
        }

        private void SettingsManager_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            int i;
            if (setts.Camera_num == 0)
            {
                setts.load_default_ini();
            }
            for (i = 0; i < setts.Camera_num; i++)
            cameraCapture.Close(i);
            cameraCapture = null;
            this.Close();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                cameraCapture.Start(1);
                checkBox2.Enabled = false;
                setts.Camera_num++;
            }
            else
            {
                cameraCapture.Close(1);
                checkBox2.Enabled = true;
                setts.Camera_num--;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
                Bitmap img;

                img = (Bitmap)cameraCapture.GetCameraPic(0);
                Pen p = new Pen(Color.Red, 1);
                Graphics g = Graphics.FromImage(img);
                //g = pictureBox1.CreateGraphics();
                g.DrawEllipse(p, setts.CameraCenterX0 - setts.Radius0, setts.CameraCenterY0 - setts.Radius0, setts.Radius0 * 2, setts.Radius0 * 2);
                g.DrawEllipse(p, setts.CameraCenterX0 - 1, setts.CameraCenterY0 - 1, 2, 2);
            
                pictureBox1.Image = (Bitmap)img;

                if (setts.Camera_num == 2)
                {
                    img = (Bitmap)cameraCapture.GetCameraPic(1);
                    g = Graphics.FromImage(img);
                    //g = pictureBox1.CreateGraphics();
                    g.DrawEllipse(p, setts.CameraCenterX1 - setts.Radius1, setts.CameraCenterY1 - setts.Radius1, setts.Radius1 * 2, setts.Radius1 * 2);
                    g.DrawEllipse(p, setts.CameraCenterX1 - 1, setts.CameraCenterY1 - 1, 2, 2);
            
                    pictureBox2.Image = (Bitmap)img;
                }

        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            setts.CameraCenterX0 = trackBar1.Value;
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            setts.CameraCenterY0 = trackBar2.Value;
        }

        private void trackBar3_Scroll(object sender, EventArgs e)
        {
            setts.Radius0 = trackBar3.Value;
        }

        private void trackBar6_Scroll(object sender, EventArgs e)
        {
            setts.CameraCenterX1 = trackBar6.Value;
        }

        private void trackBar5_Scroll(object sender, EventArgs e)
        {
            setts.CameraCenterY1 = trackBar5.Value;
        }

        private void trackBar4_Scroll(object sender, EventArgs e)
        {
            setts.Radius1 = trackBar4.Value;
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            timer1.Interval = (int)numericUpDown1.Value;
            setts.Camera_delay = (int)numericUpDown1.Value;
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            setts.Rows = (int)numericUpDown2.Value;
        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            setts.Cols = (int)numericUpDown3.Value;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (setts.Camera_num > 0) pictureBox1.Image.Save("eye1.bmp");
            if (setts.Camera_num > 1) pictureBox2.Image.Save("eye2.bmp");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            setts.save_ini();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            setts.load_ini();

            trackBar1.Value = setts.CameraCenterX0;
            trackBar2.Value = setts.CameraCenterY0;
            trackBar3.Value = setts.Radius0;

            trackBar6.Value = setts.CameraCenterX1;
            trackBar5.Value = setts.CameraCenterY1;
            trackBar4.Value = setts.Radius1;

            numericUpDown1.Value = setts.Camera_delay;
            numericUpDown2.Value = setts.Rows;
            numericUpDown3.Value = setts.Cols;

        }
    }
}